<html>
<head><meta charset="utf-8"><title>Option of struct with single niche · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html">Option of struct with single niche</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206660825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206660825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206660825">(Aug 12 2020 at 05:18)</a>:</h4>
<p>Is there a guarantee that with a struct like:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">VecLike</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">ptr</span>: <span class="nc">NonNull</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">length</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">capacity</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">_marker</span>:<span class="nc">PhantomData</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>That an <code>Option&lt;VecLike&lt;T&gt;&gt;</code> has the same size and alignment as <code>VecLike&lt;T&gt;</code>, and that it represents <code>None</code> by storing null in the <code>ptr</code> field?<br>
I would ask the same question about any other struct that has exactly one field with niches, and that field has exactly one niche.</p>



<a name="206671538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206671538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206671538">(Aug 12 2020 at 08:41)</a>:</h4>
<p>I think this is currently not covered by the relevant RFC, and not even by the non-normative text in <a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#discriminant-elision-on-option-like-enums">https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#discriminant-elision-on-option-like-enums</a></p>



<a name="206673687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673687">(Aug 12 2020 at 09:10)</a>:</h4>
<p>It looks like it's covered by the non-normative text there.</p>
<blockquote>
<p>Option-like enums where the payload defines at least one niche value are guaranteed to be represented using the same memory layout as their payload. This is called discriminant elision, as there is no explicit discriminant value stored anywhere. Instead, niche values are used to represent the unit variant.</p>
</blockquote>
<p>Given the struct has only one field with niche values and that field has one niche value, it must follow that it is that niche value that is used to represent the unit variant.</p>
<p>You could argue that the niche values are actually the niche value of the field with the niche value crossed with any value for the memory used by length and capacity. But for the purpose of checking Noneness, you only need to check whether that field is in its niche value. But for the purpose of creating a Non value, this doesn't necessarily hold because the compiler could decide that only one of those combination of other fields is valid for being None (e.g. length and capacity also being set to 0 in VecLike&lt;T&gt;).</p>



<a name="206673735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673735">(Aug 12 2020 at 09:11)</a>:</h4>
<blockquote>
<p>Given the struct has only one field with niche values and that field has one niche value, it must follow that it is that niche value that is used to represent the unit variant.</p>
</blockquote>
<p>the text talks about "option-like enums"</p>



<a name="206673741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673741">(Aug 12 2020 at 09:11)</a>:</h4>
<p>I dont think a struct can qualify ;)</p>



<a name="206673806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673806">(Aug 12 2020 at 09:12)</a>:</h4>
<p>oh wait this is about <code>Option&lt;MyStruct&gt;</code></p>



<a name="206673830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673830">(Aug 12 2020 at 09:12)</a>:</h4>
<p>and we do not say explicitly that a struct where at least one field has a niche also has a niche, but that seems like a reasonable expectation</p>



<a name="206673856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673856">(Aug 12 2020 at 09:13)</a>:</h4>
<p>(though it is not clear what happens when there are multiple niches of different size -- I dont think we <em>guarantee</em> that the struct will use the biggest)</p>



<a name="206673920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206673920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206673920">(Aug 12 2020 at 09:14)</a>:</h4>
<p>but since all we care about is "at least one niche value", I retract what I said earlier -- <code>VecLike</code> has "at least one niche value" and thus you should be able to rely on the optimization. <em>based on what UCG says</em> anyway, which is <em>not normative</em>, it is just something that we hoped to put into an RFC eventually.</p>



<a name="206675462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206675462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206675462">(Aug 12 2020 at 09:41)</a>:</h4>
<p>The stdlib docs says it's only guaranteed for <code>Option&lt;Box&lt;T&gt;&gt;</code> but the UCG feels de facto even if it's not de jure.</p>



<a name="206677796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206677796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206677796">(Aug 12 2020 at 10:14)</a>:</h4>
<p>certainly that's how it is implemented currently, but like all layout details, it may change any time</p>



<a name="206677820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206677820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206677820">(Aug 12 2020 at 10:14)</a>:</h4>
<p>if people start to rely on what rustc does currently just because we have a document saying that more clearly than the code, we'll all be very sad and we'll have to... dunno, hide such documents from the public in future or so? that doesn't even seem possible.^^</p>



<a name="206681914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206681914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206681914">(Aug 12 2020 at 11:14)</a>:</h4>
<p>I wanted to rely on the general version of this optimisation a couple of days ago, but my reading of the std lib is that it's not even <em>guaranteed</em> for <code>Option&lt;Box&lt;T&gt;&gt;</code>: it reads more to me as "at the moment it's optimised to a single pointer" (unlike, say, <code>Vec</code>, which makes explicit guarantees using language like "is and always will be").</p>



<a name="206682396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206682396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206682396">(Aug 12 2020 at 11:21)</a>:</h4>
<p>the only guarantees that currently exist are what has been FCP'd in <a href="https://github.com/rust-lang/rust/pull/60300">https://github.com/rust-lang/rust/pull/60300</a></p>



<a name="206682434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206682434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206682434">(Aug 12 2020 at 11:21)</a>:</h4>
<p>so indeed it's not guaranteed for <code>Box</code></p>



<a name="206682481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206682481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206682481">(Aug 12 2020 at 11:22)</a>:</h4>
<p>but it is for e.g. <code>Option&lt;&amp;T&gt;</code> or <code>Option&lt;NonNull&lt;T&gt;&gt;</code></p>



<a name="206685862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206685862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206685862">(Aug 12 2020 at 12:12)</a>:</h4>
<p>agreed</p>



<a name="206686004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206686004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206686004">(Aug 12 2020 at 12:14)</a>:</h4>
<p>i wonder if <code>Option</code>'s docs <a href="https://doc.rust-lang.org/std/option/index.html">https://doc.rust-lang.org/std/option/index.html</a> should point at <a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#discriminant-elision-on-option-like-enums">https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#discriminant-elision-on-option-like-enums</a>, dropping the slightly vague text "This usage of Option to create safe nullable pointers is so common that Rust does special optimizations to make the representation of Option&lt;Box&lt;T&gt;&gt; a single pointer. Optional pointers in Rust are stored as efficiently as any other pointer type." ?</p>



<a name="206686770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206686770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206686770">(Aug 12 2020 at 12:25)</a>:</h4>
<p>hm, funny that the docs actually speak about <code>Box</code> for which this is not guaranteed...</p>



<a name="206687275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687275">(Aug 12 2020 at 12:30)</a>:</h4>
<p>even better, they mention that it's optimised this way, but not in a way that guarantees it :) if i raise a pr to tweak this, would you be interested in looking at it?</p>



<a name="206687666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687666">(Aug 12 2020 at 12:35)</a>:</h4>
<p>I wouldnt link to the UCG docs, as they are non-normative</p>



<a name="206687693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687693">(Aug 12 2020 at 12:35)</a>:</h4>
<p>but what you could do if you wanted is to change the docs to say that this is explicitly guaranteed. and then we can ask the lang-team to give their seal of approval.</p>



<a name="206687725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687725">(Aug 12 2020 at 12:35)</a>:</h4>
<p>that would add <code>Box</code> to the list of types for which this is guarnateed, which seems uncontroversial</p>



<a name="206687734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687734">(Aug 12 2020 at 12:36)</a>:</h4>
<p>might even be good to just have that list in the docs</p>



<a name="206687830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687830">(Aug 12 2020 at 12:36)</a>:</h4>
<blockquote>
<p>they mention that it's optimised this way, but not in a way that guarantees it :)</p>
</blockquote>
<p>wdym? to me if the docs say it it sounds like it is guaranteed. which is an accident I think, though I doubt anyone actually wants to not guarantee it.</p>



<a name="206687845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206687845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206687845">(Aug 12 2020 at 12:37)</a>:</h4>
<p>the reason it's not guarnateed is that nobody bothered to spell out the guarantee yet.^^</p>



<a name="206696134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206696134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206696134">(Aug 12 2020 at 13:52)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> are you suggest just making the text about <code>Option&lt;Box&lt;T&gt;&gt;</code> clearer or also porting over the concepts of <code>Option&lt;&amp;T&gt;</code> and so on?</p>



<a name="206696225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206696225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206696225">(Aug 12 2020 at 13:53)</a>:</h4>
<p>yes. I am suggesting to say explicitly that we guarantee this optimization for everything listed at <a href="https://github.com/rust-lang/rust/pull/60300">https://github.com/rust-lang/rust/pull/60300</a>, and <code>Box</code>.</p>



<a name="206696256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206696256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206696256">(Aug 12 2020 at 13:53)</a>:</h4>
<p>agreed</p>



<a name="206702250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206702250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206702250">(Aug 12 2020 at 14:35)</a>:</h4>
<p>does the Reference have any info about this optimization?</p>



<a name="206702939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206702939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206702939">(Aug 12 2020 at 14:41)</a>:</h4>
<p>dont know</p>



<a name="206703446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206703446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206703446">(Aug 12 2020 at 14:45)</a>:</h4>
<p>if T-lang guarantee this optimization, does document it in Reference make sense?</p>



<a name="206703480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206703480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurence Tratt <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206703480">(Aug 12 2020 at 14:45)</a>:</h4>
<p>from memory i don't think it's referenced there</p>



<a name="206703717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206703717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206703717">(Aug 12 2020 at 14:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209117">lzutao</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche/near/206703446">said</a>:</p>
<blockquote>
<p>if T-lang guarantee this optimization, does document it in Reference make sense?</p>
</blockquote>
<p>yes</p>



<a name="206704068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206704068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206704068">(Aug 12 2020 at 14:50)</a>:</h4>
<p>Are other codegen backends able to offer this optimization? Or it is an optimization in rustc?</p>



<a name="206704368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206704368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206704368">(Aug 12 2020 at 14:52)</a>:</h4>
<p>its waaay pre-codegen</p>



<a name="206704381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206704381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206704381">(Aug 12 2020 at 14:52)</a>:</h4>
<p>it's part of layout computation</p>



<a name="206704428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206704428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206704428">(Aug 12 2020 at 14:53)</a>:</h4>
<p>every codegen backend has to be able to handle every possible layout that rustc computes, and thus has to support this optimization</p>



<a name="206704795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206704795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206704795">(Aug 12 2020 at 14:56)</a>:</h4>
<p>nice</p>



<a name="206734647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option%20of%20struct%20with%20single%20niche/near/206734647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Option.20of.20struct.20with.20single.20niche.html#206734647">(Aug 12 2020 at 18:48)</a>:</h4>
<p>I look forward to the safe transmute work for this, because it's looking like that will start to put these guarantees into the trait system, which is a much clearer way to say "yup, we've committed to that one but not that other one" than text in UCG</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>